package com.wang.decagency.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.decagency.entity.SysBase;
import com.wang.decagency.entity.SysCustomer;
import com.wang.decagency.service.impl.SysCustomerServiceImpl;
import com.wang.utils.ResultEntity;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wang
 * @since 2023-05-29
 */
@Slf4j
@Api(tags = "客户管理")
@RestController
@RequestMapping("/decagency/customer")
public class SysCustomerController {

    @Autowired
    private SysCustomerServiceImpl customerService;

    @PostMapping("/getCustomerList")
    @ApiOperation(value = "查询所有客户", notes = "查询所有客户")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功")})
    public Object getCustomerList(@RequestBody Map<String, Object> mapParam) throws Exception {
        SysCustomer customer = JSON.parseObject(JSON.toJSONString(mapParam), SysCustomer.class);
        log.info("customer:" + JSON.toJSONString(customer));
        // 从params中获取分页参数
        int pageNum = mapParam.get("pageNum") != null ? (int) mapParam.get("pageNum") : 1;
        int pageSize = mapParam.get("pageSize") != null ? (int) mapParam.get("pageSize") : 5;
        log.info(JSON.toJSONString("pageNum:" + pageNum));
        // 创建QueryWrapper对象
        QueryWrapper<SysCustomer> queryWrapper = new QueryWrapper<>();
        // 添加like条件
        if (customer.getCustomerName() != null) {
            queryWrapper.like("customer_name", customer.getCustomerName());
        }
        Page<SysCustomer> page = new Page<>(pageNum, pageSize);
        IPage<SysCustomer> SysUserList = customerService.page(page, queryWrapper);
        return ResultEntity.ok(SysUserList);
    }


    @PostMapping("/addCustomer")
    @ApiOperation(value = "添加用户", notes = "添加一个新的用户")
    public Object addCustomer(@RequestBody @ApiParam(name = "customer", value = "用户信息", required = true) SysCustomer customer) {
        if (Objects.isNull(customer) || customer.getCustomerName() == null) {
            return ResultEntity.fail("入参不全");
        }
        customerService.saveOrUpdate(customer);
        return ResultEntity.ok();
    }


    @PostMapping("/getByKey")
    @ApiOperation(value = "查询详情", notes = "查询详情")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功")})
    public Object getByKey(@RequestBody @ApiParam(name = "cost", value = "报关单信息") SysCustomer obj) throws Exception {
        obj = customerService.getById(obj.getCustomerId());
        return ResultEntity.ok(obj);
    }

    @PostMapping("/delByKeys")
    @ApiOperation(value = "根据主键删除", notes = "根据主键删除")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功")})
    public Object delByKeys(@RequestBody List<Integer> ids) throws Exception {
        boolean b = customerService.removeByIds(ids);
        return ResultEntity.ok(b);
    }

}
